package com.qiantu.phone.ui.activity

import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import com.hjq.base.BaseDialog
import com.qiantu.api.utils.ThemeUtil
import com.qiantu.phone.R
import com.qiantu.phone.app.AppActivity
import com.qiantu.phone.app.AppApplication
import com.qiantu.phone.ui.adapter.CardTransformer
import com.qiantu.phone.ui.adapter.ThemeViewpagerAdapter
import com.qiantu.phone.ui.dialog.ConfirmHintDialog
import kotlinx.android.synthetic.main.activity_switch_theme.*

class SwitchThemeActivity : AppActivity() {

    val data = ArrayList<Int>()
    var positionOffset = 0

    override fun getLayoutId(): Int {
        return R.layout.activity_switch_theme
    }

    override fun initView() {
        val themeViewpagerAdapter = ThemeViewpagerAdapter(this)
        data.add(R.mipmap.img_colorchoice_blue)
        data.add(R.mipmap.img_colorchoice_blackgold)
        data.add(R.mipmap.img_colorchoice_orange)
        positionOffset = 100000 * data.size
        themeViewpagerAdapter.data = data
        viewPager.adapter = themeViewpagerAdapter
        viewPager.setPageTransformer(CardTransformer())
        val recyclerView = viewPager.getChildAt(0) as RecyclerView
        val paddingLeftRight = resources.getDimensionPixelSize(R.dimen.dp_70)
        recyclerView.setPadding(paddingLeftRight, resources.getDimensionPixelSize(R.dimen.dp_38), paddingLeftRight, resources.getDimensionPixelSize(R.dimen.dp_52))
        recyclerView.clipToPadding = false
        viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                when (position % data.size) {
                    0 -> {
                        btnSwitch.setBackgroundResource(R.drawable.btn_ff3e86f7_round_4dp)
                        topBg.setBackgroundResource(R.drawable.switch_theme_ff3e86f7_top_bg)
                    }
                    1 -> {
                        btnSwitch.setBackgroundResource(R.drawable.btn_000000_round_4dp)
                        topBg.setBackgroundResource(R.drawable.switch_theme_ffe1c07c_top_bg)
                    }
                    2 -> {
                        btnSwitch.setBackgroundResource(R.drawable.btn_fd9e32_round_4dp)
                        topBg.setBackgroundResource(R.drawable.switch_theme_ffeca24b_top_bg)
                    }
                }
            }
        })
        setOnClickListener(btnSwitch)
        when (ThemeUtil.themeId) {
            ThemeUtil.THEME_DEFAULT -> viewPager.currentItem = positionOffset
            ThemeUtil.THEME_BLACK_GOLD -> viewPager.currentItem = positionOffset + 1
            ThemeUtil.THEME_ORANGE -> viewPager.currentItem = positionOffset + 2
        }
    }

    override fun initData() {

    }

    override fun onClick(view: View?) {
        if (view != null) {
            when(view.id){
                R.id.btnSwitch -> showSwitchThemeDialog()
            }
        }
    }

    var mConfirmHintDialog: BaseDialog? = null
    var mConfirmHintDialogBuilder: ConfirmHintDialog.Builder? = null
    private fun showSwitchThemeDialog() {
        var rightBtnBackgroundResId = R.drawable.btn_ff3e86f7_round_4dp
        when(viewPager.currentItem % data.size){
            0 -> rightBtnBackgroundResId = R.drawable.btn_ff3e86f7_round_4dp
            1 -> rightBtnBackgroundResId = R.drawable.btn_000000_round_4dp
            2 -> rightBtnBackgroundResId = R.drawable.btn_fd9e32_round_4dp
        }
        if (mConfirmHintDialog == null) {
            mConfirmHintDialogBuilder = ConfirmHintDialog.Builder(context)
            mConfirmHintDialogBuilder!!.setLeftBtnText(resources.getString(R.string.cancel))
            mConfirmHintDialogBuilder!!.setRightBtnText(resources.getString(R.string.ok))
            mConfirmHintDialogBuilder!!.setRightBtnBackground(rightBtnBackgroundResId)
            mConfirmHintDialogBuilder!!.setContent(resources.getString(R.string.restart_take_effect))
            mConfirmHintDialogBuilder!!.setListener(object : ConfirmHintDialog.OnListener {
                override fun onLeftClick(dialog: BaseDialog) {
                    dialog.dismiss()
                }

                override fun onConfirm(dialog: BaseDialog) {
                    dialog.dismiss()
                    when (viewPager.currentItem % data.size) {
                        0 -> AppApplication.getInstance().saveThemeIdAndReset(ThemeUtil.THEME_DEFAULT)
                        1 -> AppApplication.getInstance().saveThemeIdAndReset(ThemeUtil.THEME_BLACK_GOLD)
                        2 -> AppApplication.getInstance().saveThemeIdAndReset(ThemeUtil.THEME_ORANGE)
                    }
                }
            })
            mConfirmHintDialog = mConfirmHintDialogBuilder!!.create()
        }
        mConfirmHintDialogBuilder!!.setRightBtnBackground(rightBtnBackgroundResId)
        mConfirmHintDialog!!.show()
    }

    override fun isStatusBarDarkFont(): Boolean {
        return false
    }
}